blob: 8a88a69a7d824f06906e8e636b83059614f531cf [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Adrian Roos90462222016-02-17 15:45:09 -080019import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010020import com.android.internal.logging.nano.MetricsProto;
Adrian Roos90462222016-02-17 15:45:09 -080021
Jacek Surazskif5b9c722009-05-18 12:09:59 +020022import android.content.ActivityNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020024import android.content.Intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.res.Resources;
Adrian Roosa85a2c62016-01-26 09:14:22 -080026import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.Handler;
28import android.os.Message;
Adrian Roos890f1f32016-03-03 16:23:30 -080029import android.text.BidiFormatter;
Joe Onorato8a9b2202010-02-26 18:56:32 -080030import android.util.Slog;
Adrian Roosa85a2c62016-01-26 09:14:22 -080031import android.view.LayoutInflater;
32import android.view.View;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070033import android.view.WindowManager;
Adrian Roosa85a2c62016-01-26 09:14:22 -080034import android.widget.FrameLayout;
35import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Adrian Roosa85a2c62016-01-26 09:14:22 -080037final class AppNotRespondingDialog extends BaseErrorDialog implements View.OnClickListener {
Jacek Surazskif5b9c722009-05-18 12:09:59 +020038 private static final String TAG = "AppNotRespondingDialog";
39
40 // Event 'what' codes
41 static final int FORCE_CLOSE = 1;
42 static final int WAIT = 2;
43 static final int WAIT_AND_REPORT = 3;
44
Adrian Roos90462222016-02-17 15:45:09 -080045 public static final int CANT_SHOW = -1;
46 public static final int ALREADY_SHOWING = -2;
47
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 private final ActivityManagerService mService;
49 private final ProcessRecord mProc;
Adrian Roosa85a2c62016-01-26 09:14:22 -080050
Adrian Roosc3a06e52018-02-16 18:33:17 +010051 public AppNotRespondingDialog(ActivityManagerService service, Context context, Data data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 super(context);
Adrian Roosa85a2c62016-01-26 09:14:22 -080053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 mService = service;
Adrian Roosc3a06e52018-02-16 18:33:17 +010055 mProc = data.proc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 Resources res = context.getResources();
Adrian Roosa85a2c62016-01-26 09:14:22 -080057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 setCancelable(false);
59
60 int resid;
Adrian Roosc3a06e52018-02-16 18:33:17 +010061 CharSequence name1 = data.activity != null
62 ? data.activity.info.loadLabel(context.getPackageManager())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 : null;
64 CharSequence name2 = null;
Adrian Roosc3a06e52018-02-16 18:33:17 +010065 if ((mProc.pkgList.size() == 1) &&
66 (name2=context.getPackageManager().getApplicationLabel(mProc.info)) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 if (name1 != null) {
68 resid = com.android.internal.R.string.anr_activity_application;
69 } else {
70 name1 = name2;
Adrian Roosc3a06e52018-02-16 18:33:17 +010071 name2 = mProc.processName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 resid = com.android.internal.R.string.anr_application_process;
73 }
74 } else {
75 if (name1 != null) {
Adrian Roosc3a06e52018-02-16 18:33:17 +010076 name2 = mProc.processName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 resid = com.android.internal.R.string.anr_activity_process;
78 } else {
Adrian Roosc3a06e52018-02-16 18:33:17 +010079 name1 = mProc.processName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 resid = com.android.internal.R.string.anr_process;
81 }
82 }
83
Adrian Roos890f1f32016-03-03 16:23:30 -080084 BidiFormatter bidi = BidiFormatter.getInstance();
85
Adrian Roosa85a2c62016-01-26 09:14:22 -080086 setTitle(name2 != null
Adrian Roos890f1f32016-03-03 16:23:30 -080087 ? res.getString(resid, bidi.unicodeWrap(name1.toString()), bidi.unicodeWrap(name2.toString()))
88 : res.getString(resid, bidi.unicodeWrap(name1.toString())));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
Adrian Roosc3a06e52018-02-16 18:33:17 +010090 if (data.aboveSystem) {
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070091 getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
92 }
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070093 WindowManager.LayoutParams attrs = getWindow().getAttributes();
Adrian Roosc3a06e52018-02-16 18:33:17 +010094 attrs.setTitle("Application Not Responding: " + mProc.info.processName);
Adam Lesinski95c42972013-10-02 10:13:27 -070095 attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR |
96 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070097 getWindow().setAttributes(attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 }
99
Adrian Roosa85a2c62016-01-26 09:14:22 -0800100 @Override
101 protected void onCreate(Bundle savedInstanceState) {
102 super.onCreate(savedInstanceState);
Alan Viverette51efddb2017-04-05 10:00:01 -0400103 final FrameLayout frame = findViewById(android.R.id.custom);
Adrian Roosa85a2c62016-01-26 09:14:22 -0800104 final Context context = getContext();
105 LayoutInflater.from(context).inflate(
106 com.android.internal.R.layout.app_anr_dialog, frame, true);
107
Alan Viverette51efddb2017-04-05 10:00:01 -0400108 final TextView report = findViewById(com.android.internal.R.id.aerr_report);
Adrian Roosa85a2c62016-01-26 09:14:22 -0800109 report.setOnClickListener(this);
110 final boolean hasReceiver = mProc.errorReportReceiver != null;
111 report.setVisibility(hasReceiver ? View.VISIBLE : View.GONE);
Alan Viverette51efddb2017-04-05 10:00:01 -0400112 final TextView close = findViewById(com.android.internal.R.id.aerr_close);
Adrian Roosa85a2c62016-01-26 09:14:22 -0800113 close.setOnClickListener(this);
Alan Viverette51efddb2017-04-05 10:00:01 -0400114 final TextView wait = findViewById(com.android.internal.R.id.aerr_wait);
Adrian Roosa85a2c62016-01-26 09:14:22 -0800115 wait.setOnClickListener(this);
116
117 findViewById(com.android.internal.R.id.customPanel).setVisibility(View.VISIBLE);
118 }
119
120 @Override
121 public void onClick(View v) {
122 switch (v.getId()) {
123 case com.android.internal.R.id.aerr_report:
124 mHandler.obtainMessage(WAIT_AND_REPORT).sendToTarget();
125 break;
126 case com.android.internal.R.id.aerr_close:
127 mHandler.obtainMessage(FORCE_CLOSE).sendToTarget();
128 break;
129 case com.android.internal.R.id.aerr_wait:
130 mHandler.obtainMessage(WAIT).sendToTarget();
131 break;
132 default:
133 break;
134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136
137 private final Handler mHandler = new Handler() {
138 public void handleMessage(Message msg) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200139 Intent appErrorIntent = null;
Adrian Roos90462222016-02-17 15:45:09 -0800140
141 MetricsLogger.action(getContext(), MetricsProto.MetricsEvent.ACTION_APP_ANR,
142 msg.what);
143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 switch (msg.what) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200145 case FORCE_CLOSE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 // Kill the application.
Dan Egnor42471dd2010-01-07 17:25:22 -0800147 mService.killAppAtUsersRequest(mProc, AppNotRespondingDialog.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 break;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200149 case WAIT_AND_REPORT:
150 case WAIT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 // Continue waiting for the application.
152 synchronized (mService) {
153 ProcessRecord app = mProc;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200154
155 if (msg.what == WAIT_AND_REPORT) {
Adrian Roos20d7df32016-01-12 18:59:43 +0100156 appErrorIntent = mService.mAppErrors.createAppErrorIntentLocked(app,
Jacek Surazski41a9fd52010-01-27 16:37:21 -0800157 System.currentTimeMillis(), null);
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200158 }
159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 app.notResponding = false;
161 app.notRespondingReport = null;
162 if (app.anrDialog == AppNotRespondingDialog.this) {
163 app.anrDialog = null;
164 }
Dianne Hackborn2be00932013-09-22 16:46:00 -0700165 mService.mServices.scheduleServiceTimeoutLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 }
167 break;
168 }
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200169
170 if (appErrorIntent != null) {
171 try {
172 getContext().startActivity(appErrorIntent);
173 } catch (ActivityNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800174 Slog.w(TAG, "bug report receiver dissappeared", e);
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200175 }
176 }
Adrian Roosa85a2c62016-01-26 09:14:22 -0800177
178 dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
180 };
Adrian Roosc3a06e52018-02-16 18:33:17 +0100181
182 static class Data {
183 final ProcessRecord proc;
184 final ActivityRecord activity;
185 final boolean aboveSystem;
186
187 Data(ProcessRecord proc, ActivityRecord activity, boolean aboveSystem) {
188 this.proc = proc;
189 this.activity = activity;
190 this.aboveSystem = aboveSystem;
191 }
192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193}