AppErrorDialog.java revision 9046222cb2b1bd57278ddbf71d9f628f8dd254ae
1/*
2 * Copyright (C) 2006 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.am;
18
19import android.app.ActivityManagerInternal;
20import android.app.ActivityOptions;
21import android.content.Context;
22import android.content.pm.IPackageDataObserver;
23import android.content.pm.PackageManager;
24import android.content.res.Resources;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.Message;
28import android.util.Slog;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.WindowManager;
32import android.widget.FrameLayout;
33import android.widget.TextView;
34
35import java.util.List;
36
37import static com.android.server.am.ActivityManagerService.IS_USER_BUILD;
38
39final class AppErrorDialog extends BaseErrorDialog implements View.OnClickListener {
40
41    private final ActivityManagerService mService;
42    private final AppErrorResult mResult;
43    private final ProcessRecord mProc;
44    private final boolean mRepeating;
45
46    private CharSequence mName;
47
48    static int CANT_SHOW = -1;
49    static int BACKGROUND_USER = -2;
50    static int ALREADY_SHOWING = -3;
51
52    // Event 'what' codes
53    static final int FORCE_QUIT = 1;
54    static final int FORCE_QUIT_AND_REPORT = 2;
55    static final int RESTART = 3;
56    static final int RESET = 4;
57    static final int MUTE = 5;
58    static final int TIMEOUT = 6;
59
60    // 5-minute timeout, then we automatically dismiss the crash dialog
61    static final long DISMISS_TIMEOUT = 1000 * 60 * 5;
62
63    public AppErrorDialog(Context context, ActivityManagerService service, Data data) {
64        super(context);
65        Resources res = context.getResources();
66
67        mService = service;
68        mProc = data.proc;
69        mResult = data.result;
70        mRepeating = data.repeating;
71        if ((mProc.pkgList.size() == 1) &&
72                (mName = context.getPackageManager().getApplicationLabel(mProc.info)) != null) {
73            setTitle(res.getString(
74                    mRepeating ? com.android.internal.R.string.aerr_application_repeated
75                            : com.android.internal.R.string.aerr_application,
76                    mName.toString(), mProc.info.processName));
77        } else {
78            mName = mProc.processName;
79            setTitle(res.getString(
80                    mRepeating ? com.android.internal.R.string.aerr_process_repeated
81                            : com.android.internal.R.string.aerr_process,
82                    mName.toString()));
83        }
84
85        setCancelable(false);
86
87        WindowManager.LayoutParams attrs = getWindow().getAttributes();
88        attrs.setTitle("Application Error: " + mProc.info.processName);
89        attrs.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR
90                | WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
91        getWindow().setAttributes(attrs);
92        if (mProc.persistent) {
93            getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
94        }
95
96        // After the timeout, pretend the user clicked the quit button
97        mHandler.sendMessageDelayed(
98                mHandler.obtainMessage(TIMEOUT),
99                DISMISS_TIMEOUT);
100    }
101
102    @Override
103    protected void onCreate(Bundle savedInstanceState) {
104        super.onCreate(savedInstanceState);
105        final FrameLayout frame = (FrameLayout) findViewById(android.R.id.custom);
106        final Context context = getContext();
107        LayoutInflater.from(context).inflate(
108                com.android.internal.R.layout.app_error_dialog, frame, true);
109
110        final TextView restart = (TextView) findViewById(com.android.internal.R.id.aerr_restart);
111        restart.setOnClickListener(this);
112        restart.setVisibility(!mRepeating ? View.VISIBLE : View.GONE);
113        final TextView reset = (TextView) findViewById(com.android.internal.R.id.aerr_reset);
114        reset.setOnClickListener(this);
115        reset.setVisibility(mRepeating ? View.VISIBLE : View.GONE);
116        final TextView report = (TextView) findViewById(com.android.internal.R.id.aerr_report);
117        report.setOnClickListener(this);
118        final boolean hasReceiver = mProc.errorReportReceiver != null;
119        report.setVisibility(hasReceiver ? View.VISIBLE : View.GONE);
120        final TextView close = (TextView) findViewById(com.android.internal.R.id.aerr_close);
121        close.setOnClickListener(this);
122        final TextView mute = (TextView) findViewById(com.android.internal.R.id.aerr_mute);
123        mute.setOnClickListener(this);
124        mute.setVisibility(!IS_USER_BUILD ? View.VISIBLE : View.GONE);
125
126        findViewById(com.android.internal.R.id.customPanel).setVisibility(View.VISIBLE);
127    }
128
129    private final Handler mHandler = new Handler() {
130        public void handleMessage(Message msg) {
131            final int result = msg.what;
132
133            synchronized (mService) {
134                if (mProc != null && mProc.crashDialog == AppErrorDialog.this) {
135                    mProc.crashDialog = null;
136                }
137            }
138            mResult.set(result);
139
140            // Make sure we don't have time timeout still hanging around.
141            removeMessages(TIMEOUT);
142
143            dismiss();
144        }
145    };
146
147    @Override
148    public void dismiss() {
149        if (!mResult.mHasResult) {
150            // We are dismissing and the result has not been set...go ahead and set.
151            mResult.set(FORCE_QUIT);
152        }
153        super.dismiss();
154    }
155
156    @Override
157    public void onClick(View v) {
158        switch (v.getId()) {
159            case com.android.internal.R.id.aerr_restart:
160                mHandler.obtainMessage(RESTART).sendToTarget();
161                break;
162            case com.android.internal.R.id.aerr_reset:
163                mHandler.obtainMessage(RESET).sendToTarget();
164                break;
165            case com.android.internal.R.id.aerr_report:
166                mHandler.obtainMessage(FORCE_QUIT_AND_REPORT).sendToTarget();
167                break;
168            case com.android.internal.R.id.aerr_close:
169                mHandler.obtainMessage(FORCE_QUIT).sendToTarget();
170                break;
171            case com.android.internal.R.id.aerr_mute:
172                mHandler.obtainMessage(MUTE).sendToTarget();
173                break;
174            default:
175                break;
176        }
177    }
178
179    static class Data {
180        AppErrorResult result;
181        TaskRecord task;
182        boolean repeating;
183        ProcessRecord proc;
184    }
185}
186