blob: c6befd7fbb7aa7eba2a301ec8e138993f5d94d10 [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;
20import com.android.internal.logging.MetricsProto;
21
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.DialogInterface;
25import android.content.Intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.res.Resources;
Adrian Roosa85a2c62016-01-26 09:14:22 -080027import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.os.Handler;
29import android.os.Message;
Adrian Roos890f1f32016-03-03 16:23:30 -080030import android.text.BidiFormatter;
Joe Onorato8a9b2202010-02-26 18:56:32 -080031import android.util.Slog;
Adrian Roosa85a2c62016-01-26 09:14:22 -080032import android.view.LayoutInflater;
33import android.view.View;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070034import android.view.WindowManager;
Adrian Roosa85a2c62016-01-26 09:14:22 -080035import android.widget.FrameLayout;
36import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Adrian Roosa85a2c62016-01-26 09:14:22 -080038import static com.android.server.am.ActivityManagerService.IS_USER_BUILD;
39
40final class AppNotRespondingDialog extends BaseErrorDialog implements View.OnClickListener {
Jacek Surazskif5b9c722009-05-18 12:09:59 +020041 private static final String TAG = "AppNotRespondingDialog";
42
43 // Event 'what' codes
44 static final int FORCE_CLOSE = 1;
45 static final int WAIT = 2;
46 static final int WAIT_AND_REPORT = 3;
47
Adrian Roos90462222016-02-17 15:45:09 -080048 public static final int CANT_SHOW = -1;
49 public static final int ALREADY_SHOWING = -2;
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 private final ActivityManagerService mService;
52 private final ProcessRecord mProc;
Adrian Roosa85a2c62016-01-26 09:14:22 -080053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 public AppNotRespondingDialog(ActivityManagerService service, Context context,
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070055 ProcessRecord app, ActivityRecord activity, boolean aboveSystem) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 super(context);
Adrian Roosa85a2c62016-01-26 09:14:22 -080057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 mService = service;
59 mProc = app;
60 Resources res = context.getResources();
Adrian Roosa85a2c62016-01-26 09:14:22 -080061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 setCancelable(false);
63
64 int resid;
65 CharSequence name1 = activity != null
66 ? activity.info.loadLabel(context.getPackageManager())
67 : null;
68 CharSequence name2 = null;
69 if ((app.pkgList.size() == 1) &&
70 (name2=context.getPackageManager().getApplicationLabel(app.info)) != null) {
71 if (name1 != null) {
72 resid = com.android.internal.R.string.anr_activity_application;
73 } else {
74 name1 = name2;
75 name2 = app.processName;
76 resid = com.android.internal.R.string.anr_application_process;
77 }
78 } else {
79 if (name1 != null) {
80 name2 = app.processName;
81 resid = com.android.internal.R.string.anr_activity_process;
82 } else {
83 name1 = app.processName;
84 resid = com.android.internal.R.string.anr_process;
85 }
86 }
87
Adrian Roos890f1f32016-03-03 16:23:30 -080088 BidiFormatter bidi = BidiFormatter.getInstance();
89
Adrian Roosa85a2c62016-01-26 09:14:22 -080090 setTitle(name2 != null
Adrian Roos890f1f32016-03-03 16:23:30 -080091 ? res.getString(resid, bidi.unicodeWrap(name1.toString()), bidi.unicodeWrap(name2.toString()))
92 : res.getString(resid, bidi.unicodeWrap(name1.toString())));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070094 if (aboveSystem) {
95 getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
96 }
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070097 WindowManager.LayoutParams attrs = getWindow().getAttributes();
98 attrs.setTitle("Application Not Responding: " + app.info.processName);
Adam Lesinski95c42972013-10-02 10:13:27 -070099 attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR |
100 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700101 getWindow().setAttributes(attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 }
103
Adrian Roosa85a2c62016-01-26 09:14:22 -0800104 @Override
105 protected void onCreate(Bundle savedInstanceState) {
106 super.onCreate(savedInstanceState);
107 final FrameLayout frame = (FrameLayout) findViewById(android.R.id.custom);
108 final Context context = getContext();
109 LayoutInflater.from(context).inflate(
110 com.android.internal.R.layout.app_anr_dialog, frame, true);
111
112 final TextView report = (TextView) findViewById(com.android.internal.R.id.aerr_report);
113 report.setOnClickListener(this);
114 final boolean hasReceiver = mProc.errorReportReceiver != null;
115 report.setVisibility(hasReceiver ? View.VISIBLE : View.GONE);
116 final TextView close = (TextView) findViewById(com.android.internal.R.id.aerr_close);
117 close.setOnClickListener(this);
118 final TextView wait = (TextView) findViewById(com.android.internal.R.id.aerr_wait);
119 wait.setOnClickListener(this);
120
121 findViewById(com.android.internal.R.id.customPanel).setVisibility(View.VISIBLE);
122 }
123
124 @Override
125 public void onClick(View v) {
126 switch (v.getId()) {
127 case com.android.internal.R.id.aerr_report:
128 mHandler.obtainMessage(WAIT_AND_REPORT).sendToTarget();
129 break;
130 case com.android.internal.R.id.aerr_close:
131 mHandler.obtainMessage(FORCE_CLOSE).sendToTarget();
132 break;
133 case com.android.internal.R.id.aerr_wait:
134 mHandler.obtainMessage(WAIT).sendToTarget();
135 break;
136 default:
137 break;
138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 }
140
141 private final Handler mHandler = new Handler() {
142 public void handleMessage(Message msg) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200143 Intent appErrorIntent = null;
Adrian Roos90462222016-02-17 15:45:09 -0800144
145 MetricsLogger.action(getContext(), MetricsProto.MetricsEvent.ACTION_APP_ANR,
146 msg.what);
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 switch (msg.what) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200149 case FORCE_CLOSE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 // Kill the application.
Dan Egnor42471dd2010-01-07 17:25:22 -0800151 mService.killAppAtUsersRequest(mProc, AppNotRespondingDialog.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 break;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200153 case WAIT_AND_REPORT:
154 case WAIT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 // Continue waiting for the application.
156 synchronized (mService) {
157 ProcessRecord app = mProc;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200158
159 if (msg.what == WAIT_AND_REPORT) {
Adrian Roos20d7df32016-01-12 18:59:43 +0100160 appErrorIntent = mService.mAppErrors.createAppErrorIntentLocked(app,
Jacek Surazski41a9fd52010-01-27 16:37:21 -0800161 System.currentTimeMillis(), null);
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200162 }
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 app.notResponding = false;
165 app.notRespondingReport = null;
166 if (app.anrDialog == AppNotRespondingDialog.this) {
167 app.anrDialog = null;
168 }
Dianne Hackborn2be00932013-09-22 16:46:00 -0700169 mService.mServices.scheduleServiceTimeoutLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 }
171 break;
172 }
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200173
174 if (appErrorIntent != null) {
175 try {
176 getContext().startActivity(appErrorIntent);
177 } catch (ActivityNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800178 Slog.w(TAG, "bug report receiver dissappeared", e);
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200179 }
180 }
Adrian Roosa85a2c62016-01-26 09:14:22 -0800181
182 dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 }
184 };
185}