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