1/*
2 * Copyright (C) 2013 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.server.telecom.components;
18
19import com.android.server.telecom.R;
20
21import android.app.Activity;
22import android.app.AlertDialog;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.os.Bundle;
26import android.telecom.Log;
27
28// TODO: Needed for move to system service: import com.android.internal.R;
29
30/**
31 * Used to display an error dialog from within the Telecom service when an outgoing call fails
32 */
33public class ErrorDialogActivity extends Activity {
34    private static final String TAG = ErrorDialogActivity.class.getSimpleName();
35
36    public static final String SHOW_MISSING_VOICEMAIL_NO_DIALOG_EXTRA = "show_missing_voicemail";
37    public static final String ERROR_MESSAGE_ID_EXTRA = "error_message_id";
38    public static final String ERROR_MESSAGE_STRING_EXTRA = "error_message_string";
39
40    /**
41     * Intent action to bring up Voicemail Provider settings.
42     */
43    public static final String ACTION_ADD_VOICEMAIL =
44            "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
45
46    @Override
47    protected void onCreate(Bundle savedInstanceState) {
48        super.onCreate(savedInstanceState);
49        final boolean showVoicemailDialog = getIntent().getBooleanExtra(
50                SHOW_MISSING_VOICEMAIL_NO_DIALOG_EXTRA, false);
51
52        if (showVoicemailDialog) {
53            showMissingVoicemailErrorDialog();
54        }  else if (getIntent().getCharSequenceExtra(ERROR_MESSAGE_STRING_EXTRA) != null) {
55            final CharSequence error = getIntent().getCharSequenceExtra(
56                    ERROR_MESSAGE_STRING_EXTRA);
57            showGenericErrorDialog(error);
58        } else {
59            final int error = getIntent().getIntExtra(ERROR_MESSAGE_ID_EXTRA, -1);
60            if (error == -1) {
61                Log.w(TAG, "ErrorDialogActivity called with no error type extra.");
62                finish();
63            } else {
64                showGenericErrorDialog(error);
65            }
66        }
67    }
68
69    private void showGenericErrorDialog(CharSequence msg) {
70        final DialogInterface.OnClickListener clickListener;
71        final DialogInterface.OnCancelListener cancelListener;
72
73        clickListener = new DialogInterface.OnClickListener() {
74            @Override
75            public void onClick(DialogInterface dialog, int which) {
76                finish();
77            }
78        };
79
80        cancelListener = new DialogInterface.OnCancelListener() {
81            @Override
82            public void onCancel(DialogInterface dialog) {
83                finish();
84            }
85        };
86
87        final AlertDialog errorDialog = new AlertDialog.Builder(this)
88                .setMessage(msg).setPositiveButton(android.R.string.ok, clickListener)
89                        .setOnCancelListener(cancelListener).create();
90
91        errorDialog.show();
92    }
93
94    private void showGenericErrorDialog(int resid) {
95        final CharSequence msg = getResources().getText(resid);
96        showGenericErrorDialog(msg);
97    }
98
99    private void showMissingVoicemailErrorDialog() {
100        new AlertDialog.Builder(this)
101                .setTitle(R.string.no_vm_number)
102                .setMessage(R.string.no_vm_number_msg)
103                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
104                        @Override
105                        public void onClick(DialogInterface dialog, int which) {
106                            finish();
107                        }})
108                .setNegativeButton(R.string.add_vm_number_str,
109                        new DialogInterface.OnClickListener() {
110                                @Override
111                                public void onClick(DialogInterface dialog, int which) {
112                                    addVoiceMailNumberPanel(dialog);
113                                }})
114                .setOnCancelListener(new DialogInterface.OnCancelListener() {
115                        @Override
116                        public void onCancel(DialogInterface dialog) {
117                            finish();
118                        }}).show();
119    }
120
121
122    private void addVoiceMailNumberPanel(DialogInterface dialog) {
123        if (dialog != null) {
124            dialog.dismiss();
125        }
126
127        // Navigate to the Voicemail setting in the Call Settings activity.
128        Intent intent = new Intent(ACTION_ADD_VOICEMAIL);
129        startActivity(intent);
130        finish();
131    }
132
133    @Override
134    public void finish() {
135        super.finish();
136        // Don't show the return to previous task animation to avoid showing a black screen.
137        // Just dismiss the dialog and undim the previous activity immediately.
138        overridePendingTransition(0, 0);
139    }
140}
141