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