blob: cb76e2fafebdc804274311caec2435e32b710d8c [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
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070019import android.content.pm.ApplicationInfo;
Adrian Roos90462222016-02-17 15:45:09 -080020import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010021import com.android.internal.logging.nano.MetricsProto;
Adrian Roos90462222016-02-17 15:45:09 -080022
Jacek Surazskif5b9c722009-05-18 12:09:59 +020023import android.content.ActivityNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020025import 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 -080038final class AppNotRespondingDialog extends BaseErrorDialog implements View.OnClickListener {
Jacek Surazskif5b9c722009-05-18 12:09:59 +020039 private static final String TAG = "AppNotRespondingDialog";
40
41 // Event 'what' codes
42 static final int FORCE_CLOSE = 1;
43 static final int WAIT = 2;
44 static final int WAIT_AND_REPORT = 3;
45
Adrian Roos90462222016-02-17 15:45:09 -080046 public static final int CANT_SHOW = -1;
47 public static final int ALREADY_SHOWING = -2;
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 private final ActivityManagerService mService;
50 private final ProcessRecord mProc;
Adrian Roosa85a2c62016-01-26 09:14:22 -080051
Adrian Roosc3a06e52018-02-16 18:33:17 +010052 public AppNotRespondingDialog(ActivityManagerService service, Context context, Data data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 super(context);
Adrian Roosa85a2c62016-01-26 09:14:22 -080054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 mService = service;
Adrian Roosc3a06e52018-02-16 18:33:17 +010056 mProc = data.proc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 Resources res = context.getResources();
Adrian Roosa85a2c62016-01-26 09:14:22 -080058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 setCancelable(false);
60
61 int resid;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070062 CharSequence name1 = data.aInfo != null
63 ? data.aInfo.loadLabel(context.getPackageManager())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 : null;
65 CharSequence name2 = null;
Adrian Roosc3a06e52018-02-16 18:33:17 +010066 if ((mProc.pkgList.size() == 1) &&
67 (name2=context.getPackageManager().getApplicationLabel(mProc.info)) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 if (name1 != null) {
69 resid = com.android.internal.R.string.anr_activity_application;
70 } else {
71 name1 = name2;
Adrian Roosc3a06e52018-02-16 18:33:17 +010072 name2 = mProc.processName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 resid = com.android.internal.R.string.anr_application_process;
74 }
75 } else {
76 if (name1 != null) {
Adrian Roosc3a06e52018-02-16 18:33:17 +010077 name2 = mProc.processName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 resid = com.android.internal.R.string.anr_activity_process;
79 } else {
Adrian Roosc3a06e52018-02-16 18:33:17 +010080 name1 = mProc.processName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 resid = com.android.internal.R.string.anr_process;
82 }
83 }
84
Adrian Roos890f1f32016-03-03 16:23:30 -080085 BidiFormatter bidi = BidiFormatter.getInstance();
86
Adrian Roosa85a2c62016-01-26 09:14:22 -080087 setTitle(name2 != null
Adrian Roos890f1f32016-03-03 16:23:30 -080088 ? res.getString(resid, bidi.unicodeWrap(name1.toString()), bidi.unicodeWrap(name2.toString()))
89 : res.getString(resid, bidi.unicodeWrap(name1.toString())));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
Adrian Roosc3a06e52018-02-16 18:33:17 +010091 if (data.aboveSystem) {
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070092 getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
93 }
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070094 WindowManager.LayoutParams attrs = getWindow().getAttributes();
Adrian Roosc3a06e52018-02-16 18:33:17 +010095 attrs.setTitle("Application Not Responding: " + mProc.info.processName);
Adam Lesinski95c42972013-10-02 10:13:27 -070096 attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR |
97 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070098 getWindow().setAttributes(attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
100
Adrian Roosa85a2c62016-01-26 09:14:22 -0800101 @Override
102 protected void onCreate(Bundle savedInstanceState) {
103 super.onCreate(savedInstanceState);
Alan Viverette51efddb2017-04-05 10:00:01 -0400104 final FrameLayout frame = findViewById(android.R.id.custom);
Adrian Roosa85a2c62016-01-26 09:14:22 -0800105 final Context context = getContext();
106 LayoutInflater.from(context).inflate(
107 com.android.internal.R.layout.app_anr_dialog, frame, true);
108
Alan Viverette51efddb2017-04-05 10:00:01 -0400109 final TextView report = findViewById(com.android.internal.R.id.aerr_report);
Adrian Roosa85a2c62016-01-26 09:14:22 -0800110 report.setOnClickListener(this);
111 final boolean hasReceiver = mProc.errorReportReceiver != null;
112 report.setVisibility(hasReceiver ? View.VISIBLE : View.GONE);
Alan Viverette51efddb2017-04-05 10:00:01 -0400113 final TextView close = findViewById(com.android.internal.R.id.aerr_close);
Adrian Roosa85a2c62016-01-26 09:14:22 -0800114 close.setOnClickListener(this);
Alan Viverette51efddb2017-04-05 10:00:01 -0400115 final TextView wait = findViewById(com.android.internal.R.id.aerr_wait);
Adrian Roosa85a2c62016-01-26 09:14:22 -0800116 wait.setOnClickListener(this);
117
118 findViewById(com.android.internal.R.id.customPanel).setVisibility(View.VISIBLE);
119 }
120
121 @Override
122 public void onClick(View v) {
123 switch (v.getId()) {
124 case com.android.internal.R.id.aerr_report:
125 mHandler.obtainMessage(WAIT_AND_REPORT).sendToTarget();
126 break;
127 case com.android.internal.R.id.aerr_close:
128 mHandler.obtainMessage(FORCE_CLOSE).sendToTarget();
129 break;
130 case com.android.internal.R.id.aerr_wait:
131 mHandler.obtainMessage(WAIT).sendToTarget();
132 break;
133 default:
134 break;
135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 }
137
138 private final Handler mHandler = new Handler() {
139 public void handleMessage(Message msg) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200140 Intent appErrorIntent = null;
Adrian Roos90462222016-02-17 15:45:09 -0800141
142 MetricsLogger.action(getContext(), MetricsProto.MetricsEvent.ACTION_APP_ANR,
143 msg.what);
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 switch (msg.what) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200146 case FORCE_CLOSE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 // Kill the application.
Dan Egnor42471dd2010-01-07 17:25:22 -0800148 mService.killAppAtUsersRequest(mProc, AppNotRespondingDialog.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 break;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200150 case WAIT_AND_REPORT:
151 case WAIT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 // Continue waiting for the application.
153 synchronized (mService) {
154 ProcessRecord app = mProc;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200155
156 if (msg.what == WAIT_AND_REPORT) {
Adrian Roos20d7df32016-01-12 18:59:43 +0100157 appErrorIntent = mService.mAppErrors.createAppErrorIntentLocked(app,
Jacek Surazski41a9fd52010-01-27 16:37:21 -0800158 System.currentTimeMillis(), null);
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200159 }
160
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700161 app.setNotResponding(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 app.notRespondingReport = null;
163 if (app.anrDialog == AppNotRespondingDialog.this) {
164 app.anrDialog = null;
165 }
Dianne Hackborn2be00932013-09-22 16:46:00 -0700166 mService.mServices.scheduleServiceTimeoutLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
168 break;
169 }
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200170
171 if (appErrorIntent != null) {
172 try {
173 getContext().startActivity(appErrorIntent);
174 } catch (ActivityNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800175 Slog.w(TAG, "bug report receiver dissappeared", e);
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200176 }
177 }
Adrian Roosa85a2c62016-01-26 09:14:22 -0800178
179 dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 }
181 };
Adrian Roosc3a06e52018-02-16 18:33:17 +0100182
183 static class Data {
184 final ProcessRecord proc;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -0700185 final ApplicationInfo aInfo;
Adrian Roosc3a06e52018-02-16 18:33:17 +0100186 final boolean aboveSystem;
187
Wale Ogunwale51cc98a2018-10-15 10:41:05 -0700188 Data(ProcessRecord proc, ApplicationInfo aInfo, boolean aboveSystem) {
Adrian Roosc3a06e52018-02-16 18:33:17 +0100189 this.proc = proc;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -0700190 this.aInfo = aInfo;
Adrian Roosc3a06e52018-02-16 18:33:17 +0100191 this.aboveSystem = aboveSystem;
192 }
193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194}