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